package com.cloudfunx.common.exception;

import lombok.Data;

/**
 * @author linjiayu
 * @date 2019-08-11
 */
@Data
public class BizResponse<T> {
    private Integer code;
    private String msg;
    private T data;

    public BizResponse() {
        this.code = 200;
    }

    public BizResponse(T data) {
        this.code = 200;
        this.data = data;
    }

    public BizResponse(BizRetStatus bizRetStatus) {
        this(bizRetStatus.getCode(), bizRetStatus.getMsg());
    }

    public BizResponse(BizRetStatus bizRetStatus, String message) {
        this(bizRetStatus.getCode(), message);
    }

    public BizResponse(Integer code, String msg) {
        this(code, msg, null);
    }

    public BizResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> T check(BizResponse<T> response) throws BizException {
        Integer successCode = 200;
        if (!successCode.equals(response.getCode())) {
            throw new BizException(response.getMsg());
        }
        return response.getData();
    }

    public static BizResponse<Void> success() {
        return new BizResponse<>();
    }

    public static <T> BizResponse<T> success(T data) {
        return new BizResponse<>(data);
    }
}
